package controller

import (
	"blog/dao"
	"github.com/julienschmidt/httprouter"
	"html/template"
	"net/http"
	"strconv"
)

//文章阅读页
func ReadHandle(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
	dao.CommonAction(r, w)

	id, err := strconv.ParseInt(ps.ByName("id"), 10, 64)
	if err != nil {
		dao.LogError(err)
		dao.ShowErrorPage("内容未找到", 403, w)
		return
	}

	info, err := dao.GetPostInfo(id, false)
	if err != nil {
		dao.ShowErrorPage("内容未找到", 403, w)
		return
	}

	if info.IsTemp == 1 || info.IsShow == 0 || info.IsPrivate == 1 {
		dao.ShowErrorPage("页面未找到", 404, w)
		return
	}

	t, err := template.New("read.html").Funcs(dao.FuncMap).ParseFiles("templates/read.html", "templates/pub_inc.html")
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	param := struct {
		Info  dao.PostData
		QRUrl template.HTML
	}{
		Info:  info,
		QRUrl: template.HTML(dao.GetCurrentUrl(r) + "?fr=qrCode"),
	}

	t.Execute(w, param)

	//更新阅读数
	dao.IncPostReadNum(id)

}
